package com.first.xiinn.action;

import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import com.first.core.common.QueryFilter;
import com.first.core.web.action.BaseAction;
import com.first.xiinn.model.Dictionary;
import com.first.xiinn.service.DictionaryService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DictionaryAction extends BaseAction
{

  @Resource
  private DictionaryService dictionaryService;
  private Dictionary dictionary;
  private String dicId;
  private String itemName;

  public String getItemName()
  {
    return this.itemName;
  }

  public void setItemName(String itemName) {
    this.itemName = itemName;
  }

  public String getDicId() {
    return this.dicId;
  }

  public void setDicId(String dicId) {
    this.dicId = dicId;
  }

  public Dictionary getDictionary() {
    return this.dictionary;
  }

  public void setDictionary(Dictionary dictionary) {
    this.dictionary = dictionary;
  }

  public String list()
  {
    QueryFilter filter = new QueryFilter(getRequest());
    List<Dictionary> list = this.dictionaryService.getAll(filter);

    Type type = new TypeToken<List<Dictionary>>(){}.getType();
    StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
      .append(filter.getPagingBean().getTotalItems()).append(",result:");

    Gson gson = new Gson();
    buff.append(gson.toJson(list, type));
    buff.append("}");

    this.jsonString = buff.toString();

    return "success";
  }

  public String load()
  {
    List<String> list = this.dictionaryService.getAllByItemName(this.itemName);
    StringBuffer buff = new StringBuffer("[");
    for (String itemName : list) {
      buff.append("'").append(itemName).append("',");
    }
    if (list.size() > 0) {
      buff.deleteCharAt(buff.length() - 1);
    }
    buff.append("]");
    setJsonString(buff.toString());
    return "success";
  }

  public String multiDel()
  {
    String[] ids = getRequest().getParameterValues("ids");
    if (ids != null) {
      for (String id : ids) {
        this.dictionaryService.remove(id);
      }
    }

    this.jsonString = "{success:true}";

    return "success";
  }

  public String get()
  {
    Dictionary dictionary = (Dictionary)this.dictionaryService.get(this.dicId);

    Gson gson = new Gson();

    StringBuffer sb = new StringBuffer("{success:true,data:");
    sb.append(gson.toJson(dictionary));
    sb.append("}");
    setJsonString(sb.toString());

    return "success";
  }

  public String save()
  {
    this.dictionaryService.save(this.dictionary);
    setJsonString("{success:true}");
    return "success";
  }

  public String items()
  {
    List<Dictionary> list = this.dictionaryService.getAllItems();
    StringBuffer buff = new StringBuffer("[");
    for (Dictionary dictionary : list) {
      buff.append("[");	
      buff.append("'").append(dictionary.getItemTypeValue()).append("'");
      buff.append("],");
    }
    if (list.size() > 0) {
      buff.deleteCharAt(buff.length() - 1);
    }
    buff.append("]");
    setJsonString(buff.toString());
    return "success";
  }
}
